﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using Sunny.UI;
using SuperMap.Data;
using SuperMap.Realspace;
using SuperMap.UI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Ycjw.PipeLine.WPF;

namespace Ycjw.PipeLine.BigScreen
{
    public partial class FullScreeFrameworkEx2 : UIForm, IResposeWPF
    {
        public UIPanel GetCenterPanel()
        {
            return this.uiPanelCenter;
        }
        #region 构造数据
        public void InitData(int tag = 100)
        {
            if (m_FloatWindow == null) return;
            InitData1(tag);
            InitData2(tag);
            InitData3(tag);
            InitData4(tag);
        }
        private void InitData1(int tag = 100)
        {
            m_FloatWindow.StackPanel01.Children.Clear();
            AngularGauge cc = new AngularGauge
            {
                Value = 1.7*tag,
                FromValue = 50,
                ToValue = 2.5*tag,
                LabelsStep = 50,
                TicksStep = 25,
                Wedge = 300,
                TicksForeground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Yellow),
                Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.BlanchedAlmond),
                FontWeight = new System.Windows.FontWeight(),
                FontSize = 16,
                SectionsInnerRadius = .5
            };
            m_FloatWindow.StackPanel01.Children.Add(cc);
            cc.Sections.Add(new AngularSection() { FromValue = 50, ToValue = 200, Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Gold) });
            cc.Sections.Add(new AngularSection() { FromValue = 200, ToValue = 250, Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.AliceBlue) });
        }
        private void InitData2(int tag = 100)
        {
            m_FloatWindow.StackPanel02.Children.Clear();
            PieChart cc = new PieChart
            {
                LegendLocation = LegendLocation.Right,
                ChartLegend = new DefaultLegend()
                {
                    Foreground = System.Windows.Media.Brushes.White
                }
            };
            m_FloatWindow.StackPanel02.Children.Add(cc);
            var pieSeriesCollection = new SeriesCollection
            {
                new PieSeries
                {
                    Title = "80年代",
                    Values = new ChartValues<ObservableValue> { new ObservableValue(8000*tag) },
                    DataLabels = true
                },
                new PieSeries
                {
                    Title = "90年代",
                    Values = new ChartValues<ObservableValue> { new ObservableValue(60000 * tag) },
                    DataLabels = true
                },
                new PieSeries
                {
                    Title = "00年代",
                    Values = new ChartValues<ObservableValue> { new ObservableValue(100000 * tag) },
                    DataLabels = true
                },
                new PieSeries
                {
                    Title = "10年代",
                    Values = new ChartValues<ObservableValue> { new ObservableValue(90000 * tag) },
                    DataLabels = true
                }
            };
            cc.Series.AddRange(pieSeriesCollection);
        }

        Func<double, string> FormatterT { get; set; }
        Func<double, string> FormatterS { get; set; }
        private void InitData3(int tag = 100)
        {
            m_FloatWindow.StackPanel03.Children.Clear();
            CartesianChart cc = new CartesianChart
            {
                LegendLocation = LegendLocation.Right,
                ChartLegend = new DefaultLegend()
                {
                    Foreground = System.Windows.Media.Brushes.White
                }
            };
            m_FloatWindow.StackPanel03.Children.Add(cc);
            //新建一条文件大小的柱状图
            ColumnSeries sizeseries = new ColumnSeries();
            //新建一条温度的柱状图
            ColumnSeries templeseries = new ColumnSeries();
            //添加两个纵坐标的单位
            FormatterT = value => value + "C°";
            FormatterS = value => value + "KB";

            templeseries.Title = "temp";
            //温度的柱状图依赖于第一条纵坐标
            templeseries.ScalesYAt = 0;
            Axis axisT = new Axis { Title = "temp/C°" ,LabelFormatter = FormatterT };

            sizeseries.Title = "size";
            //文件大小的柱状图依赖于第二条纵坐标
            sizeseries.ScalesYAt = 1;
            Axis axisS = new Axis { Title = "size/KB", LabelFormatter = FormatterS };

            cc.AxisY.Add(axisT);
            cc.AxisY.Add(axisS);

            List<double> temperature = new List<double>() { 100, 200, 150, 879, 34, 432 };
            for(var t = 0;t < temperature.Count;t++)
            {
                temperature[t] += tag;
            }
            templeseries.Values = new ChartValues<double>(temperature);
            List<double> filesize = new List<double>() { 0.4, 1.5, 8.9, 0.9, 5.5, 10.0 };
            for (var t = 0; t < filesize.Count; t++)
            {
                filesize[t] += tag/100;
            }
            sizeseries.Values = new ChartValues<double>(filesize);


            var SeriesCollection = new SeriesCollection { };
            SeriesCollection.Add(templeseries);
            SeriesCollection.Add(sizeseries);
            cc.Series.AddRange(SeriesCollection);
        }

        private void InitData4(int tag = 100)
        {
            m_FloatWindow.StackPanel04.Children.Clear();
            CartesianChart cc = new CartesianChart
            {
                LegendLocation = LegendLocation.Right,
                ChartLegend = new DefaultLegend()
                {
                    Foreground = System.Windows.Media.Brushes.White
                }
            };
            m_FloatWindow.StackPanel04.Children.Add(cc);
            List<string> titles = new List<string> { "高陵", "雁塔", "未央" };
            List<List<double>> values = new List<List<double>>
            {
                new List<double> { 30, 40, 60 },
                new List<double> { 20, 10, 50 },
                new List<double> { 10, 50, 30 }
            };
            for(int i = 0;i < values.Count; i++)
            {
                for(int j = 0;j < values[i].Count; j++)
                {
                    values[i][j] += tag;
                }
            }
            List<string> dates = GetCurrentMonthDates();
            SeriesCollection lineSeriesCollection = new SeriesCollection();
            List<string> lineXLabels = new List<string>();
            for (int i = 0; i < titles.Count; i++)
            {
                LineSeries lineseries = new LineSeries
                {
                    DataLabels = true,
                    Title = titles[i],
                    Values = new ChartValues<double>(values[i]),
                };
                lineXLabels.Add(dates[i]);
                lineSeriesCollection.Add(lineseries);
            }
            cc.Series.AddRange(lineSeriesCollection);
            Axis aAxis = new Axis
            {
                Labels = lineXLabels
            };
            cc.AxisX.Add(aAxis);
        }
        #endregion
        /// <summary>
        /// 获取当前月的每天的日期
        /// </summary>
        /// <returns>日期集合</returns>
        List<string> GetCurrentMonthDates()
        {
            List<string> dates = new List<string>();
            DateTime dt = DateTime.Now;
            int year = dt.Year;
            int mouth = dt.Month;
            int days = DateTime.DaysInMonth(year, mouth);
            //本月第一天时间      
            DateTime dt_First = dt.AddDays(1 - (dt.Day));
            dates.Add(String.Format("{0:d}", dt_First.Date));

            for (int i = 1; i < days; i++)
            {
                DateTime temp = dt_First.AddDays(i);
                dates.Add(String.Format("{0:d}", temp.Date));
            }
            return dates;
        }
        public SceneControl SceneControl { get; set; }
        /// <summary>
        /// 控制屏幕图层
        /// </summary>
        public void AddScreenLayer()
        {
            if (SceneControl == null) return;
            var screenLayer = this.SceneControl.Scene.ScreenLayer;
            screenLayer.Clear();
            var logoPath = Commons.Paths.ApplicationResourcesDir + "\\BigScreen\\title01.png";
            var logoWidth = 600;
            var left = (this.SceneControl.Width) / 2;
            //向三维屏幕图层中添加几何对象
            GeoPicture3D geoPicture3D = new GeoPicture3D(logoPath, new Point3D(left, 40, 0), logoWidth, 80, 0);
            screenLayer.XUnit = ScreenCoordinateUnit.Pixel;
            screenLayer.YUnit = ScreenCoordinateUnit.Pixel;
            screenLayer.Add(geoPicture3D, "logo");
            SceneControl.Refresh();
        }
        BForm m_FloatWindow;
        public FullScreeFrameworkEx2()
        {
            InitializeComponent();
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            this.WindowState = FormWindowState.Maximized;
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            if (m_FloatWindow == null) return;
            m_FloatWindow.Width = this.Width;
            m_FloatWindow.Height = this.Height;
        }
        protected override void OnLocationChanged(EventArgs e)
        {
            if (m_FloatWindow == null) return;
            m_FloatWindow.Left = this.Left;
            m_FloatWindow.Top = this.Top;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitChart();
        }
        /// <summary>
        /// 加载透明层
        /// </summary>
        void InitChart()
        {
            m_FloatWindow = new BForm(this)
            {
                ShowInTaskbar = false,
                Topmost = true,
                Left = this.Left,
                Top = this.Top,
                Width = this.Width,
                Height = this.Height
            };
            m_FloatWindow.Show();
            m_FloatWindow.IsVisibleChanged += FloatWindow_IsVisibleChanged;

            this.m_FloatWindow.EllipseAll.MouseUp += EllipseAll_MouseUp;
            this.m_FloatWindow.EllipseWS.MouseUp += EllipseWS_MouseUp;
            this.m_FloatWindow.EllipseYS.MouseUp += EllipseYS_MouseUp;
            InitData();
        }

        private void EllipseYS_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.InitData(30);
        }

        private void EllipseWS_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.InitData(80);
        }

        private void EllipseAll_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.InitData(100);
        }

        private void FloatWindow_IsVisibleChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            if (m_FloatWindow.IsVisible == false)
            {
                this.Hide();
            }
            else
            {
                this.Show();
            }
        }

        public void ResposeWPF(string tag)
        {
            switch (tag)
            {
                case "STOP_CYCLE":
                    break;
                case "VIEWENTIRE":
                    var lookAtKeys = new List<string>()
                    {
                        "Longitude","Latitude","Tilt","Range","Altitude","Heading"
                    };
                    var values = Commons.SerializeHelper.IniHelper.ReadLookAt(lookAtKeys.ToArray());
                    LookAt la = new LookAt()
                    {
                        Longitude = double.Parse(values["Longitude"]),
                        Latitude = double.Parse(values["Latitude"]),
                        Tilt = double.Parse(values["Tilt"]),
                        Range = double.Parse(values["Range"]),
                        Altitude = double.Parse(values["Altitude"]),
                        Heading = double.Parse(values["Heading"])
                    };
                    SceneControl.Scene.Fly(la,1000);
                    break;
            }
        }
                protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (m_FloatWindow == null) return;
            if(this.Visible == false)
            {
                m_FloatWindow.Hide();
            }
            else
            {
                m_FloatWindow.Show();
            }
        }
        protected override void OnClosed(EventArgs e)
        {
            if (m_FloatWindow == null) return;
            m_FloatWindow.Hide();
            base.OnClosed(e);
        }
        protected override void Dispose(bool disposing)
        {
            Hide();
        }
    }
}
